package com.passion.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Profile;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.Resource;

/**
 * description Knife4j配置类
 *
 * @author 🇨🇳中国印🇨🇳
 * @since 2022年05月26日 09:36:58
 **/
@Configuration
@EnableSwagger2
@EnableKnife4j
@Profile("!prod")
@Import(BeanValidatorPluginsConfiguration.class)
public class Knife4jConfig {

	private static final String PREFIX = "archives-admin";

	private static final String DESC = "档案管理系统";

	/**
	 * 对外开放接口所在的包
	 */
	private static final String OPEN_API_BASE_PACKAGE = "com.passion.openapi.web";


	/**
	 * 标准化接口所在的包
	 */
	private static final String STANDARD_API_BASE_PACKAGE = "com.passion.web";

	private static final String TEST_API_PACKAGE = "com.passion.test";

	@Resource
	private OpenApiExtensionResolver openApiExtensionResolver;

	/**
	 * API分组 系统Open API
	 */
	@Bean(value = PREFIX + "OpenAPI")
	public Docket apiV1() {
		String suffix = "对外开放API";
		return new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(standardApiInfo())
				.groupName(DESC + suffix)
				.select()
				.apis(RequestHandlerSelectors.basePackage(OPEN_API_BASE_PACKAGE))
				.paths(PathSelectors.any())
				.build().extensions(openApiExtensionResolver.buildExtensions(DESC + suffix));
	}

	/**
	 * API分组 产品标准API接口
	 */
	@Bean(value = PREFIX + "ProductStandardAPI")
	public Docket productStandardAPI() {
		String suffix = "标准API";
		return new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(standardApiInfo())
				.groupName(DESC + suffix)
				.select()
				.apis(RequestHandlerSelectors.basePackage(STANDARD_API_BASE_PACKAGE))
				.paths(PathSelectors.any())
				.build().extensions(openApiExtensionResolver.buildExtensions(DESC + suffix));
	}

	/**
	 * API分组 产品测试API接口
	 */
	@Bean(value = PREFIX + "ProductTestAPI")
	public Docket productTestAPI() {
		String suffix = "测试API";
		return new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(standardApiInfo())
				.groupName(DESC + suffix)
				.select()
				.apis(RequestHandlerSelectors.basePackage(TEST_API_PACKAGE))
				.paths(PathSelectors.any())
				.build().extensions(openApiExtensionResolver.buildExtensions(DESC + suffix));
	}


	/**
	 * api的信息
	 *
	 * @return {@link ApiInfo }
	 */
	private ApiInfo originApiInfo() {
		return new ApiInfoBuilder()
				.title(PREFIX + "老API 说明文档")
				.contact(new Contact("前人", "", ""))
				.description("基于Swagger2和Knife4j实现的接口文档")
				.version("before 3.1.5")
				.build();
	}

	/**
	 * api的信息
	 *
	 * @return {@link ApiInfo }
	 */
	private ApiInfo standardApiInfo() {
		return new ApiInfoBuilder()
				.title(PREFIX + "标准API 说明文档")
				.contact(new Contact("🇨🇳中国印🇨🇳", "https://gitee.com/zy-explorej2ee", "zyexplorej2ee@sina.com"))
				.description("基于Swagger2和Knife4j实现的接口文档")
				.version("3.1.5")
				.build();
	}
}